package com.example.esquel_carpool.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.esquel_carpool.R;

import com.example.esquel_carpool.entity.home.ImagesBean;
import com.example.esquel_carpool.entity.home.ListBean;
import com.example.esquel_carpool.entity.home.UserBean;
import com.example.esquel_carpool.glideModule.GlideApp;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
    private static final String TAG = "HomeAdapter";
    private List<ListBean> data = new ArrayList<>();

    private LayoutInflater mInflater;

    private Gson gson = new Gson();

    private Context context;

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public HomeAdapter(Context context, List<ListBean> data) {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
        if (data != null) {
            this.data = data;
        }
    }

    @NonNull
    @Override
    public HomeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.home_item_view, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final HomeAdapter.ViewHolder viewHolder, int i) {
        final ListBean listBean = data.get(i);
        viewHolder.titleTextView.setText(listBean.getTitle());
        viewHolder.priceTextView.setText("¥" + listBean.getPrice());
        viewHolder.descTextView.setText(listBean.getDesc());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);

        try {
            Date parse = sdf.parse(listBean.getPost_time());
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
            viewHolder.locationAndPostTimeTextView.setText(format1.format(parse) + " " + listBean.getLocation());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String images = listBean.getImages();
        images = images.replace("\\", "");
//        Log.d(TAG, "images====" + images);
        Type type = new TypeToken<List<ImagesBean>>() {}.getType();
        List<ImagesBean>imagels=gson.fromJson(images,type);
//        HomeImagesListAdapter homeImagesListAdapter = new HomeImagesListAdapter(context,imagels);
        ArrayList<ImageInfo> imageInfo = new ArrayList<>();

        if (imagels != null) {
            for (ImagesBean image : imagels) {
                ImageInfo info = new ImageInfo();
                info.setThumbnailUrl(image.getPath());
                info.setBigImageUrl(image.getPath());
                imageInfo.add(info);
            }
        }
        viewHolder.mNineGridView.setAdapter(new NineGridViewClickAdapter(context, imageInfo));
        String user_image_url=context.getResources().getString(R.string.img_url_base);
        UserBean user = listBean.getUser();
        if (user !=null){
            user_image_url=user_image_url+user.getImgpath();
            GlideApp.with(context)
                    .load(user_image_url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .error(R.drawable.ic_default_image)
                    .centerCrop()
                    .into(viewHolder.userImageView);
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener!=null){
                    onItemClickListener.onItemClick(viewHolder.getLayoutPosition(),listBean);
                }
            }
        });

//        viewHolder.mRecyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
//        viewHolder.mRecyclerView.setAdapter(homeImagesListAdapter);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        ImageView userImageView;

        TextView titleTextView, locationAndPostTimeTextView, priceTextView, descTextView;

        NineGridView mNineGridView;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            userImageView = itemView.findViewById(R.id.userImageView);

            titleTextView = itemView.findViewById(R.id.titleTextView);
            locationAndPostTimeTextView = itemView.findViewById(R.id.locationAndPostTimeTextView);
            priceTextView = itemView.findViewById(R.id.priceTextView);
            descTextView = itemView.findViewById(R.id.descTextView);
            mNineGridView = itemView.findViewById(R.id.mNineGridView);

        }
    }

    public  interface OnItemClickListener{
        void onItemClick(int pos,ListBean listBean);
    }
}
